﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BaiTu.ConfigInfo;
using BaiTu.Framework.Enums;
using BaiTu.Common;

namespace BaiTu.QueueService
{
    /// <summary>
    /// 队列的配置
    /// </summary>
    public static class QueueConfiguration
    {
        private static ConfigInfo.MSMQConfigInfo _MSMQConfigInfo;
        private static ConfigInfo.RedisConfigInfo _redisConfigInfo;
        private static ConfigInfo.RabbitMQConfigInfo _rabbitMQConfigInfo;
        private static ConfigInfo.QueueConfigInfo _queueConfigInfo;
        public static QueueType QueueType;


        public static ConfigInfo.MSMQConfigInfo MSMQConfigInfo
        {
            get
            {
                return _MSMQConfigInfo;
            }
        }

        public static ConfigInfo.RedisConfigInfo RedisConfigInfo
        {
            get
            {
                return _redisConfigInfo;
            }
        }

        public static ConfigInfo.RabbitMQConfigInfo RabbitMQConfigInfo
        {
            get
            {
                return _rabbitMQConfigInfo;
            }
        }

        static QueueConfiguration()
        {
            ///首先为queueConfigInfo 赋值
            if (_queueConfigInfo == null)
            {
                _queueConfigInfo = Config.QueueConfig.GetConfig();
                if (_queueConfigInfo == null)
                {
                    throw new Framework.Exceptions.BaiTuCoreException("queue 配置初始化失败 配置文件queue.config");
                }
            }

            if (_queueConfigInfo == null)
            {
                throw new Framework.Exceptions.BaiTuCoreException("队列的配置文件queue.config 不存在 无法执行消息队列");
            }
            if (_queueConfigInfo.QueueType.Equals("MSMQ", StringComparison.CurrentCultureIgnoreCase))
            {
                ///如果消息队列采用MSMQ进行传递
                QueueType = QueueType.MSMQ;
                _MSMQConfigInfo = Config.MSMQConfig.GetConfig();
                _MSMQConfigInfo.MustNotNull("队列采用MSMQ，但是MSMQ初始化失败");
            }
            else if (_queueConfigInfo.QueueType.Equals("REDIS", StringComparison.CurrentCultureIgnoreCase))
            {
                ///如果消息队列采用REDIS进行传递
                QueueType = QueueType.Redis;
                _redisConfigInfo = Config.RedisConfig.GetConfig();
                _redisConfigInfo.MustNotNull("队列采用redis，但是redis初始化失败");
            }
            else if (_queueConfigInfo.QueueType.Equals("RABBITMQ", StringComparison.CurrentCultureIgnoreCase))
            {
                //如果消息队列采用的是rabbitmq 
                QueueType = QueueType.RabbitMQ;
                _rabbitMQConfigInfo = Config.RabbitMQConfig.GetConfig();
                _rabbitMQConfigInfo.MustNotNull("队列采用rabbitmq，但是rabbitmq初始化失败");
            }
            else
            {
                throw new Framework.Exceptions.BaiTuCoreException("队列没有指定具体的传递介质 QueueType属性值必须为MSMQ或者Redis或者RabbitMQ");
            }

        }
    }
}
